using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Interpolation2D
{
    delegate double InterpolationFunc(double[] X, double[] Y, double x);

    class DoublePrecisionPoint
    {
        public double X { get; set; }
        public double Y { get; set; }

        public DoublePrecisionPoint()
        {
            X = Y = 0;
        }

        public DoublePrecisionPoint(double x, double y)
        {
            X = x;
            Y = y;
        }

        public double Module => Math.Sqrt(X * X + Y * Y);

        static public DoublePrecisionPoint operator -(DoublePrecisionPoint point1, DoublePrecisionPoint point2)
        {
            return new DoublePrecisionPoint { X = point2.X - point1.X, Y = point2.Y - point1.Y };
        }
    }

    /// <summary>
    /// Класс для хранения данных односвязной кривой.
    /// </summary>
    class Curve
    {
        List<DoublePrecisionPoint> _points = new List<DoublePrecisionPoint>();

        public void Add(DoublePrecisionPoint point)
        {
            _points.Add(point);
        }

        public double MaxY => _points.Max(z => z.Y);
        public double MinY => _points.Min(z => z.Y);
        public double MaxX => _points.Max(z => z.X);
        public double MinX => _points.Min(z => z.X);
        public int Count => _points.Count();
        public DoublePrecisionPoint GetPoint(int index)
        {
            return _points[index];
        }
    }

    /// <summary>
    /// Класс для построения графиков функций.
    /// </summary>
    class Interpolation2D
    {
        #region === members ===

        Curve _curve = null;

        #endregion

        public double MaxY => _curve.MaxY;
        public double MinY => _curve.MinY;
        public double MaxX => _curve.MaxX;
        public double MinX => _curve.MinX;
        public Curve curve => _curve;

        public Interpolation2D(InterpolationDescription id)
        {
            const int c_steps = 1024;
            double x1 = id.T1;
            double x2 = id.T2;

            _curve = new Curve();
            double x, y;
            double kt = (x2 - x1) / (c_steps - 1);

            for (int i = 0; i < c_steps; i++)
            {
                x = kt * i + x1;
                y = id.function(id.X, id.Y, x);

                _curve.Add(new DoublePrecisionPoint(x, y));
            }
        }
    }
}

